package me.pplay.playtalk.protocol;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.protocol.bean.KeyValue;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.protocol.bean.SendNode;
import me.pplay.playtalk.protocol.bean.TPBuddyinfo;
import me.pplay.playtalk.protocol.bean.TPPrivate;
import me.pplay.playtalk.protocol.bean.TpGroup;
import me.pplay.playtalk.protocol.bean.TpNotiNode;
import me.pplay.playtalk.protocol.bean.TpNotiNode.tp_msg_node;
import me.pplay.playtalk.protocol.bean.TpNotiNode.tp_req_node;
import me.pplay.playtalk.protocol.bean.TpNotiNode.tp_res_node;
import me.pplay.playtalk.protocol.bean.TpPacket;
import me.pplay.playtalk.service.PlayTalkService;
import me.pplay.playtalk.util.SkyUtil;
import me.pplay.playtalk.util.UserInfo;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

public class TpChat extends ProtocolPacket {
	public static final String BROADCASTACTION_TPCHAT = "me.pplay.playtalk.tpchat";
	public static final int CHAT_NETWORK_ERR = 100;
	public static final int CHAT_SESSIONID_ERR = 101;
	public static final int CHAT_AUTH_OK = 102;
	public static final int CHAT_AUTH_ERR = 103;
	public static final int CHAT_AUTH_TIMEOUT = 104;
	public static final int CHAT_CHPWD_OK = 105;
	public static final int CHAT_CHPWD_ERR = 106;
	public static final int CHAT_CHPWD_TIMEOUT = 107;
	public static final int CHAT_SETINFO_OK = 108;
	public static final int CHAT_SETINFO_ERR = 109;
	public static final int CHAT_SETINFO_TIMEOUT = 110;
	public static final int CHAT_REMOVEBUDDY_OK = 111;
	public static final int CHAT_REMOVEBUDDY_ERR = 112;
	public static final int CHAT_MOVEBUDDY_OK = 113;
	public static final int CHAT_MOVEBUDDY_ERR = 114;
	public static final int CHAT_GETINFO_OK = 115;
	public static final int CHAT_GETINFO_ERR = 116;
	public static final int CHAT_GETINFO_TIMEOUT = 117;
	public static final int CHAT_CREATECONFERENCE_OK = 118; // 创建群
	public static final int CHAT_CREATECONFERENCE_ERR = 119;
	public static final int CHAT_CONFERENCE_CREATE_TIMEOUT = 120;
	public static final int CHAT_NOTIFICATION_OK = 121;
	public static final int CHAT_GETGROUP_OK = 122;
	public static final int CHAT_GETBUDDIES_OK = 123;
	public static final int CHAT_GETCONFERENCEGROUP_OK = 124;
	public static final int CHAT_GETCONFERENCEGROUP_ERR = 125;
	public static final int CHAT_GETCONFERENCEBUDDIES_OK = 126;
	public static final int CHAT_GETCONFERENCEBUDDIES_ERR = 127;
	public static final int CHAT_GET_PRIVATE_OK = 128;
	public static final int CHAT_GET_PRIVATE_TIMEOUT = 129;
	public static final int CHAT_GET_PRIVATE_ERR = 130;
	public static final int CHAT_SET_PRIVATE_OK = 131;
	public static final int CHAT_SET_PRIVATE_TIMEOUT = 132;
	public static final int CHAT_SET_PRIVATE_ERR = 133;
	public static final int CHAT_STORE_GET_OK = 134;
	public static final int CHAT_STORE_GET_TIMEOUT = 135;
	public static final int CHAT_STORE_GET_ERR = 136;
	public static final int CHAT_STORE_ME_GET_OK = 137;
	public static final int CHAT_STORE_ME_GET_TIMEOUT = 138;
	public static final int CHAT_STORE_ME_GET_ERR = 139;
	public static final int CHAT_STORE_PURCHASE_OK = 140;
	public static final int CHAT_STORE_PURCHASE_TIMEOUT = 141;
	public static final int CHAT_STORE_PURCHASE_ERR = 142;
	public static final int CHAT_SNS_START = 143; // 没有用，只是索引
	public static final int CHAT_SNS_DELSTATUS_OK = 144;
	public static final int CHAT_SNS_DELSTATUS_ERR = 145;
	public static final int CHAT_SNS_DELSTATUS_TIMEOUT = 146;
	public static final int CHAT_SNS_DELRES_OK = 147;
	public static final int CHAT_SNS_DELRES_ERR = 148;
	public static final int CHAT_SNS_DELRES_TIMEOUT = 149;
	public static final int CHAT_SNS_ADDSTATUS_OK = 150;
	public static final int CHAT_SNS_ADDSTATUS_ERR = 151;
	public static final int CHAT_SNS_ADDSTATUS_TIMEOUT = 152;
	public static final int CHAT_SNS_URL_CSS_OK = 153;
	public static final int CHAT_SNS_URL_CRLHEADDATA_OK = 154;
	public static final int CHAT_SNS_URL_CRLITEMDATA_OK = 155;
	public static final int CHAT_SNS_URL_ERR = 156;
	public static final int CHAT_SNS_URL_TIMEOUT = 157;
	public static final int CHAT_ROOM_GETLIST_OK = 158;
	public static final int CHAT_GET_SEARCHBUDDIES_OK = 159;
	public static final int CHAT_GET_SEARCHBUDDIES_ERR = 160;
	public static final int CHAT_GET_SEARCHBUDDIES_TIMEOUT = 161;
	public static final int CHAT_GET_SEARCHSUGGESTIONS_OK = 162;
	public static final int CHAT_GET_SEARCHSUGGESTIONS_ERR = 163;
	public static final int CHAT_GET_SEARCHSUGGESTIONS_TIMEOUT = 164;
	public static final int CHAT_GETCONFERENCEBUDDIES_TIMEOUT = 170;
	
	public static final int TP_GROUP_ATTR_NORMAL = 1;
	public static final int TP_GROUP_ATTR_STRANGER = 2;
	public static final int TP_GROUP_ATTR_BALCKLIST = 3;
	public static final int TP_GROUP_ATTR_PBK = 4;

	public static final int TP_STATUS_AVAILABLE = 1;
	public static final int TP_STATUS_LOGOFF = 2;
	public static final int TP_STATUS_BRB = 3;
	public static final int TP_STATUS_BUSY = 4;

	private boolean isRun = false;
	private int skyid = -1;
	private boolean isneedKeepAlive = false;
	private int session_id = 0;
	private Map<Integer, DataCallBack> callBackMap = new HashMap<Integer, DataCallBack>();
	private LinkedList<TpGroup> chatRoomList = new LinkedList<TpGroup>();
	private LinkedList<TPBuddyinfo> searchBuddiesList = new LinkedList<TPBuddyinfo>();
	private LinkedList<TPBuddyinfo> suggestionBuddiesList = new LinkedList<TPBuddyinfo>();
	private Map<TpGroup, LinkedList<TPBuddyinfo>> chatGroupMap = new HashMap<TpGroup, LinkedList<TPBuddyinfo>>();
	private Map<TpGroup, LinkedList<TPBuddyinfo>> chatConferenceMap = new HashMap<TpGroup, LinkedList<TPBuddyinfo>>();
	private Map<Integer, String> httpAddrmap = new HashMap<Integer, String>();;
	private TPPrivate tpp = null;
	// private LinkedList<TPBuddyinfo> buddiesList = new
	// LinkedList<TPBuddyinfo>();

	public void tp_setcallback(int cmd, DataCallBack callback) {
		callBackMap.put(cmd, callback);
	}

	@Override
	public void tp_keeplive() {
		// TODO Auto-generated method stub
		if (isneedKeepAlive)
			tp_chat_keepalive();
	}

	@Override
	public void tp_recvcb(int result, TpPacket pkt, SendNode node) {
		// TODO Auto-generated method stub
		Log.v("TpChat", "tp_recvcb resulut = " + result);
		switch (result) {
		case ProtocolType.TP_ERRCODE_ERR_SERVER:
		case ProtocolType.TP_ERRCODE_ERR_TPVERSION:
			// case TP_ERRCODE_ERR_SOCKETCLOSE:
		case ProtocolType.TP_ERRCODE_ERR_SOCKETDATAERR:
		case ProtocolType.TP_ERRCODE_ERR_SOCKET:
		case ProtocolType.TP_ERRCODE_ERR_SOCKETRECV:
		case ProtocolType.TP_ERRCODE_ERR_SOCKETSEND:
		case ProtocolType.TP_ERRCODE_ERR_SOCKETCONNECT:
			Callback(CHAT_NETWORK_ERR, null, null);
			break;
		case ProtocolType.TP_ERRCODE_ERR_TIMEOUT: {
			// SendNode node = (tp_send_node *)wParam;
			// tp_packet *pkt = (tp_packet *)lParam;

			RspData rspdata = new RspData();
			rspdata.setSubid(node.getCmd());
			int subid = rspdata.getSubid();
			switch (subid) {
			case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_AUTH:
				/** < . */
				Callback(CHAT_AUTH_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETINFO:
				Callback(CHAT_GETINFO_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_GET:
				Callback(CHAT_GET_PRIVATE_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_SET:
				/** < . */
				Callback(CHAT_SET_PRIVATE_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_CREATE:
				/** < . */
				Callback(CHAT_CONFERENCE_CREATE_TIMEOUT, rspdata, null);
				break;
			// store
			case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_STORE_MTD_GET:
				/** < . */
				Callback(CHAT_STORE_GET_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_STORE_MTD_ME_GET:
				/**
				 * < .
				 */
				Callback(CHAT_STORE_ME_GET_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_STORE_MTD_PURCHASE:
				/**
				 * < .
				 */
				Callback(CHAT_STORE_PURCHASE_TIMEOUT, rspdata, null);
				break;

			case ProtocolType.ChatType.TP_SUBSERVICE_SNS_DELSTATUS:/* *< . */
				Callback(CHAT_SNS_DELSTATUS_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_SNS_ADDSTATUS:/* *< . */
				Callback(CHAT_SNS_ADDSTATUS_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_SNS_DELRES:/* *< . */
				Callback(CHAT_SNS_DELRES_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_SNS_GETURL:/* *< . */
				Callback(CHAT_SNS_URL_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SEARCH:/* *< . */
				Callback(CHAT_GET_SEARCHBUDDIES_TIMEOUT, rspdata, null);
				break;
			case ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETBUDDIES:/* *< . */
				Callback(CHAT_GETCONFERENCEBUDDIES_TIMEOUT, rspdata, null);
				break;

			// TP_SUBSERVICE_CONFERENCE_CREATE
			}
		}
			break;
		case ProtocolType.TP_ERRCODE_OK: {
			chat_packet_process(pkt);
		}
			break;
		}
	}

	@Override
	public boolean tp_sendcb(int cmd, SendNode nodedata) {
		// TODO Auto-generated method stub

		TpPacket pkt = new TpPacket();
		pkt.setService(ProtocolType.ServerId.TP_SERVICE_CHAT);
		pkt.setSubid((short) nodedata.getCmd());
		pkt.setStatus((short) 0);
		pkt.setId(nodedata.getSession_id());
		this.session_id = nodedata.getSession_id();
		return tp_send_packetex(pkt, null, this.skyid, nodedata.getData(),
				nodedata.getDatalen());
	}

	public TpChat() {
		ProtocolList sendlist = new ProtocolList();
		setProtocolPacketSendList(sendlist);
		SET_TP_FLAG(TP_NEED_RECORD);
		setNeed_bytes((short) TP_PACKET_HDRLEN);
	}

	public void tp_chat_start(String url, int port, int skyid) {
		if (!isRun) {
			isRun = true;
			this.skyid = skyid;
			this.tp_data_starturl(url, port);
		}
	}

	public void tp_chat_start(String url, int skyid) {
		if(url == null)return;
		if (!isRun) {
			this.skyid = skyid;
			isRun = true;
			String ip = new String(url);
			String[] iparray;
			iparray = ip.split(":");
			this.tp_data_starturl(iparray[0], Integer.parseInt(iparray[1]));
		}
	}

	public void tp_chat_stop() {
		isRun = false;
		skyid = -1;
		isneedKeepAlive = false;
//		private int session_id = 0;
		callBackMap.clear();
		chatRoomList.clear();
		searchBuddiesList.clear();
		suggestionBuddiesList.clear();
		
		chatGroupMap.clear();
		chatConferenceMap.clear();
		httpAddrmap.clear();
		tpp = null;
		tp_data_stop();
	}

	public void tp_chat_auth(String token, DataCallBack callback) {
		tp_setcallback(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_AUTH, callback);
		tp_chat_auth(token);
	}

	public void tp_chat_auth(String token) {// 回复
		Log.v("TpChat", "tp_chat_auth");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_token, token);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_AUTH, ProtocolType.TP_TIMOUT, data, callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_AUTH));
	}

	/**
	 * Gets this buddy's info. This will be shown on the buddy list.
	 */
	public void tp_chat_get_info(int skyid) {// 回复
		Log.v("TpChat", "tp_chat_get_info");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_skyid, "" + skyid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETINFO, ProtocolType.TP_TIMOUT, data, callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETINFO));
	}

	public void tp_chat_get_info(int skyid, DataCallBack callback) {
		tp_setcallback(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETINFO, callback);
		tp_chat_get_info(skyid);
	}
	//绑定手机号码
	public void tp_chat_bind_mobile_number(String phone)
	{
		Log.v("TpChat", "tp_chat_bind_mobile_number");
		byte[] data = null;
//		tp_setcallback(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_BINDMOBILENUM, callback);
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_pnum, phone);
		map.put(ProtocolType.Key.TP_TAG_imsi, UserInfo.getImsi());
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_BINDMOBILENUM, 0, data, callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_BINDMOBILENUM));

	}

	/**
	 * Set info
	 */
	public void tp_chat_set_info(TPBuddyinfo info) {// 回复

		Log.v("TpChat", "tp_chat_set_info");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_user, info.getName());
		map.put(ProtocolType.Key.TP_TAG_alias, info.getAlias());
		map.put(ProtocolType.Key.TP_TAG_salias, info.getServer_alias());
		map.put(ProtocolType.Key.TP_TAG_birth, info.getBirthday());
		map.put(ProtocolType.Key.TP_TAG_mcc, info.getCountry());
		map.put(ProtocolType.Key.TP_TAG_sign, info.getSignature());
		map.put(ProtocolType.Key.TP_TAG_pnum, info.getPnum());
		map.put(ProtocolType.Key.TP_TAG_sex, "" + info.getSex());
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SETINFO, 0, data);
	}

	public TPPrivate tp_chat_get_private_data() {
		return tpp;
	}

	public void tp_chat_get_private(int skyid, DataCallBack callback) {
		tp_setcallback(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_GET, callback);
		tp_chat_get_private(skyid);
	}

	/**
	 * Get private
	 */
	public void tp_chat_get_private(int skyid) {// 回复
		Log.v("TpChat", "tp_chat_get_private");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_skyid, "" + skyid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_GET, ProtocolType.TP_TIMOUT, data, callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_GET));
	}

	/**
	 * Set private
	 */
	public void tp_chat_set_private(TPPrivate info) {// 不回复
		Log.v("TpChat", "tp_chat_set_private");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_PRT_DETAIL_DIS, "" + info.getDisinfo());
		map.put(ProtocolType.Key.TP_TAG_PRT_SEARCHED, "" + info.getSrched());
		map.put(ProtocolType.Key.TP_TAG_PRT_STNG_MSG, "" + info.getStrgmsg());
		map.put(ProtocolType.Key.TP_TAG_PRT_AUTO_ACTIVE, "" + info.getAutoact());
		map.put(ProtocolType.Key.TP_TAG_PRT_WPAPER_ID, "" + info.getBg());
		map.put(ProtocolType.Key.TP_TAG_PRT_WPAPER_SRVR, "" + info.getSid());
		map.put(ProtocolType.Key.TP_TAG_PRT_WPAPER_PATH, info.getFpath());
		tpp = info;
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_SET, 0 * 1000, data, callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_SET));
	}

	/**
	 * \brief send im
	 */
	public void tp_chat_send_im(String alias, int skyid, String message) {// 不回复
		Log.v("TpChat", "tp_chat_send_im");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_alias, alias);
		map.put(ProtocolType.Key.TP_TAG_skyid, "" + skyid);
		map.put(ProtocolType.Key.TP_TAG_msg, message);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SENDIM, 0, data, null);
	}

	/**
	 * \brief send typing
	 */
	public void tp_chat_send_typing(int state) {
		return;
	}

	/**
	 * \brief change passwd
	 */
	public void tp_chat_change_passwd(String old_pass, String new_pass) {// 回复
		Log.v("TpChat", "tp_chat_change_passwd");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_pwd, SkyUtil.Base64encode(old_pass));
		map.put(ProtocolType.Key.TP_TAG_newpwd, SkyUtil.Base64encode(new_pass));
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_CHANGEPWD, ProtocolType.TP_TIMOUT, data, callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_CHANGEPWD));
	}
	public void tp_chat_change_passwd(String old_pass, String new_pass,DataCallBack callback) {// 回复
		tp_setcallback(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_CHANGEPWD, callback);
		tp_chat_change_passwd(old_pass, new_pass);
	}
	/**
	 * \brief add buddy
	 */
	public void tp_chat_add_buddy(String alias, int skyid, int gid, String msg) {// 不回复
		Log.v("TpChat", "tp_chat_add_buddy");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_alias, alias);
		map.put(ProtocolType.Key.TP_TAG_skyid, "" + skyid);
		map.put(ProtocolType.Key.TP_TAG_msg, msg);
		map.put(ProtocolType.Key.TP_TAG_gid, "" + gid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_ADDBUDDY, 0, data, null);
	}

	/**
	 * \brief remove buddy
	 */
	public void tp_chat_remove_buddy(int skyid, int gid) {// 不回复
		Log.v("TpChat", "tp_chat_remove_buddy");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();

		map.put(ProtocolType.Key.TP_TAG_skyid, "" + skyid);

		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_REMOVEBUDDY, 0, data);
		TPBuddyinfo info = tp_chat_getBuddyinfo(skyid);
		if (info != null) {
			tp_chat_removebuddy_data(info, tp_chat_getfriendsgid());
		}
	}

	public void tp_chat_move_buddy(int skyid, int gid) {// 不回复
		Log.v("TpChat", "tp_chat_move_buddy");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();

		map.put(ProtocolType.Key.TP_TAG_skyid, "" + skyid);
		map.put(ProtocolType.Key.TP_TAG_gid, "" + gid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_MOVEBUDDY, 0, data);
	}

	/**
	 * \brief add permit
	 */
	public void tp_chat_add_permit(String alias, int skyid, int gid) {// 不回复
		Log.v("TpChat", "tp_chat_add_permit");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_alias, alias);
		map.put(ProtocolType.Key.TP_TAG_skyid, "" + skyid);
		map.put(ProtocolType.Key.TP_TAG_gid, "" + gid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_ADDPERMIT, 0, data);
	}

	/**
	 * \brief add deny
	 */
	public void tp_chat_add_deny(String alias, int skyid, int gid) {// 不回复
		Log.v("TpChat", "tp_chat_add_deny");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_alias, alias);
		map.put(ProtocolType.Key.TP_TAG_skyid, "" + skyid);
		map.put(ProtocolType.Key.TP_TAG_gid, "" + gid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_ADDDENY, 0, data);
	}

	private void tp_chat_keepalive() {// 不回复
		Log.v("TpChat", "tp_chat_keepalive");
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_KEEPALIVE, 0, null);
	}

	public void tp_chat_search_friends(String condition, DataCallBack callback) {
		tp_setcallback(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SEARCH, callback);
		tp_chat_search_friends(condition);
	}

	public void tp_chat_search_friends(String condition) {// 回复
		Log.v("TpChat", "tp_chat_search_friends");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_key, condition);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SEARCH, 30 * 1000, data);
	}

	public LinkedList<TPBuddyinfo> tp_chat_search_friends_data() {
		Log.v("TpChat", "tp_chat_search_friends_data");
		if (searchBuddiesList.size() > 0)
			return searchBuddiesList;
		else
			return null;
	}

	public void tp_chat_search_suggestions() {// 不回复
		Log.v("TpChat", "tp_chat_search_suggestions");
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SERCHSUGGESTIONS, 0, null);

	}

	/**
	 * \brief get groups
	 */
	public void tp_chat_get_group() {// 不回复
		Log.v("TpChat", "tp_chat_get_group");
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETGROUP, 0, null);
	}

	/**
	 * \brief get friends
	 */
	public void tp_chat_get_friends(int gid, int gnum, int gtotal) {// 不回复
		Log.v("TpChat", "tp_chat_get_friends");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_gid, "" + gid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETFRIENDS, 0, data);
	}

	public void tp_chat_connectchat() {// 不回复
		Log.v("TpChat", "tp_chat_connectchat");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_hsman, UserInfo.getHsman());
		map.put(ProtocolType.Key.TP_TAG_hstype, UserInfo.getHstype());
		map.put(ProtocolType.Key.TP_TAG_imsi, UserInfo.getImsi());
		map.put(ProtocolType.Key.TP_TAG_imei, UserInfo.getImei());
		map.put(ProtocolType.Key.TP_TAG_mem, "" + UserInfo.getMem());
		map.put(ProtocolType.Key.TP_TAG_vmver, "" + UserInfo.getCurrentapiVersion());
		map.put(ProtocolType.Key.TP_TAG_qusiontype, "" + 2);
		// map.put(ProtocolType.Key.TP_TAG_et,SCREEN_ENTRY);
		map.put(ProtocolType.Key.TP_TAG_hsver, "460000123456789");
		map.put(ProtocolType.Key.TP_TAG_w, "" + UserInfo.getSrcw());
		map.put(ProtocolType.Key.TP_TAG_h, "" + UserInfo.getSrch());
		map.put(ProtocolType.Key.TP_TAG_appid, "" + UserInfo.getAppid());
		map.put(ProtocolType.Key.TP_TAG_appver, "" + UserInfo.getAppver());
		map.put(ProtocolType.Key.TP_TAG_lan, UserInfo.getLang());
		map.put("lanstr", UserInfo.getLang());
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_CONNECTCHAT, 0, data);
	}

	// TP_SUBSERVICE_CHAT_GETSEVERSTR
	public void tp_chat_gethttpaddr() {// 不回复
		Log.v("TpChat", "tp_chat_gethttpaddr");
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETSEVERSTR, 0, null);
	}

	public String tp_chat_gethttpaddr_data(Integer serverid) {
		Set<Integer> set = httpAddrmap.keySet();
		for (Integer key : set) {
			if (key == serverid)
				return httpAddrmap.get(key);
		}
		return null;

	}

	public void tp_chat_setstatus(int status) {// 不回复
		Log.v("TpChat", "tp_chat_setstatus");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_bstatus, "" + status);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SETSTATUS, 0, data);
	}

	public void tp_chat_get_chatroom() {
		Log.v("TpChat", "tp_chat_get_chatroom");
		chatRoomList.clear();
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETROOMGROUP, 0, null);
	}

	public LinkedList<TpGroup> tp_chat_get_chatroom_data() {
		Log.v("TpChat", "tp_chat_get_chatroom_data");
		if (chatRoomList.size() > 0)
			return chatRoomList;
		else
			return null;
	}

	public LinkedList<TPBuddyinfo> tp_chat_get_suggestion_data() {
		Log.v("TpChat", "tp_chat_get_suggestion_data");
		if (suggestionBuddiesList.size() > 0)
			return suggestionBuddiesList;
		else
			return null;
	}

	public void tp_chat_getofflinemsg() {// 不回复
		Log.v("TpChat", "tp_chat_getofflinemsg");
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETOFFLINEMSG, 0, null);
	}

	/**
	 * \brief conference create
	 */
	public void tp_conference_create(String gname) {// 回复
		Log.v("TpChat", "tp_conference_create");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_group, gname);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_CREATE, ProtocolType.TP_TIMOUT, data, callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_CREATE));
	}

	public void tp_conference_create(String gname, DataCallBack callback) {
		tp_setcallback(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_CREATE, callback);
		tp_conference_create(gname);
	}

	public void tp_conference_get_conferences() {// 不回复
		Log.v("TpChat", "tp_conference_get_conferences");
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETGROUP, 0, null);
	}

	public void tp_conference_get_buddies(int gid, int gnum, int gtotal, DataCallBack callback) {
		tp_setcallback(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETBUDDIES, callback);
		tp_conference_get_buddies( gid, gnum, gtotal);
	}
	
	public void tp_conference_get_buddies(int gid, int gnum, int gtotal) {// 回复
		Log.v("TpChat", "tp_conference_get_buddies");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_gid, "" + gid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETBUDDIES, ProtocolType.TP_TIMOUT, data, callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETBUDDIES));
	}

	public void tp_conference_send_im(String alias, int gid, String message) {// 不回复
		Log.v("TpChat", "tp_conference_send_im");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_alias, alias);
		map.put(ProtocolType.Key.TP_TAG_msg, message);
		map.put(ProtocolType.Key.TP_TAG_gid, "" + gid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_SENDIM, 0, data);
	}

	public void tp_conference_add_buddy(String alias, int skyid, int gid) {// 不回复
		Log.v("TpChat", "tp_conference_add_buddy");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_alias, alias);
		map.put(ProtocolType.Key.TP_TAG_skyid, "" + skyid);
		map.put(ProtocolType.Key.TP_TAG_gid, "" + gid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_ADDBUDDY, 0, data);
	}

	public void tp_conference_remove_buddy(String alias, int skyid, int gid) {// 不回复
		Log.v("TpChat", "tp_conference_remove_buddy");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_alias, alias);
		map.put(ProtocolType.Key.TP_TAG_skyid, "" + skyid);
		map.put(ProtocolType.Key.TP_TAG_gid, "" + gid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_REMOVEBUDDY, 0, data);
	}

	public void tp_conference_dissolve(int gid) {// NULL,服务器自动解散，
	}

	public void tp_conference_add_permit(String alias, int skyid, int gid) {// NULL
	}

	public void tp_conference_add_deny(String alias, int skyid, int gid) {// NULL
	}

	public void tp_conference_get_info(int gid) {// NULL
	}

	public void tp_conference_set_info() {// NULL
	}

	public void tp_conference_req_addconference(int gid) {// NULL
	}

	public void tp_conference_req_removeconference(int gid) {// NULL
	}

	public void tp_conference_quit_conference(String alias, int gid) {

		Log.v("TpChat", "tp_conference_quit_conference");
		byte[] data = null;
		Map<String, String> map = new HashMap<String, String>();
		map.put(ProtocolType.Key.TP_TAG_alias, alias);
		map.put(ProtocolType.Key.TP_TAG_gid, "" + gid);
		data = tp_packet_putdata(map);
		tp_chat_senddata_cmn(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_QUIT, 0, data);
		TpGroup g1 = tp_chat_conferenceIsExist(gid);
		if (g1 != null) {
			chatConferenceMap.remove(g1);
		}

	}

	private void tp_chat_senddata_cmn(int cmd, int timeout, byte[] data, DataCallBack callBack) {
		ProtocolList list = getProtocolPacketSendList();
		if (!isRun) {

			tp_chat_start(Globals.ChatIpString, Globals.skyid);
		}
		list.tp_data_append_send(cmd, timeout, data, callBack);
	}

	private void tp_chat_senddata_cmn(int cmd, int timeout, byte[] data) {
		ProtocolList list = getProtocolPacketSendList();
		list.tp_data_append_send(cmd, timeout, data, null);
	}

	public TpGroup tp_chat_conferenceIsExist(int gid) {
		Map<TpGroup, LinkedList<TPBuddyinfo>> map = chatConferenceMap;
		if (gid < 0)
			return null;
		Set<TpGroup> set = map.keySet();
		for (TpGroup key : set) {
			if (key.getId() == gid) {
				return (key);
			}
		}
		return null;
	}

	public TpGroup tp_chat_getConferencebyid(int gid) {
		Map<TpGroup, LinkedList<TPBuddyinfo>> map = chatConferenceMap;
		if (gid < 0)
			return null;
		Set<TpGroup> set = map.keySet();
		for (TpGroup key : set) {
			if (key.getId() == gid) {
				return key;
			}
		}
		return null;
	}
	
	public LinkedList<TPBuddyinfo> tp_chat_getConferenceBuddiesList(int gid) {
		Map<TpGroup, LinkedList<TPBuddyinfo>> map = chatConferenceMap;
		if (gid < 0)
			return null;
		Set<TpGroup> set = map.keySet();
		for (TpGroup key : set) {
			if (key.getId() == gid) {
				return map.get(key);
			}
		}
		return null;
	}

	public LinkedList<TPBuddyinfo> tp_chat_getbuddieslist(int gid) {
		Map<TpGroup, LinkedList<TPBuddyinfo>> map = chatGroupMap;
		if (gid < 0)
			return null;
		Set<TpGroup> set = map.keySet();
		for (TpGroup key : set) {
			if (key.getId() == gid) {
				return map.get(key);
			}
		}
		return null;
	}

	public LinkedList<TPBuddyinfo> tp_chat_getfriendslist() {
		Map<TpGroup, LinkedList<TPBuddyinfo>> map = chatGroupMap;
		Set<TpGroup> set = map.keySet();
		for (TpGroup key : set) {
			if (key.getAttr() == TP_GROUP_ATTR_NORMAL) {
				return map.get(key);
			}
		}
		return null;
	}
	public LinkedList<TPBuddyinfo> tp_chat_ignorelist() {
		Map<TpGroup, LinkedList<TPBuddyinfo>> map = chatGroupMap;
		Set<TpGroup> set = map.keySet();
		for (TpGroup key : set) {
			if (key.getAttr() == TP_GROUP_ATTR_BALCKLIST) {
				return map.get(key);
			}
		}
		return null;
	}
	public TPBuddyinfo tp_chat_getfriendsInfo(int skyid) {
		LinkedList<TPBuddyinfo> list = tp_chat_getfriendslist();
		if (list != null) {
			for (TPBuddyinfo key : list) {
				if (key.getSkyid() == skyid) {
					return key;
				}
			}
		}
		return null;
	}

	public int tp_chat_getfriendsgid() {
		Map<TpGroup, LinkedList<TPBuddyinfo>> map = chatGroupMap;
		Set<TpGroup> set = map.keySet();
		for (TpGroup key : set) {
			if (key.getAttr() == TP_GROUP_ATTR_NORMAL) {
				return key.getId();
			}
		}
		return -1;
	}

	public boolean tp_chat_removebuddy_data(TPBuddyinfo info, int gid) {
		LinkedList<TPBuddyinfo> list = tp_chat_getbuddieslist(gid);
		if (list != null) {
			return list.remove(info);
		}
		return false;
	}

	public int tp_chat_getfriendsBackListGid() {
		Map<TpGroup, LinkedList<TPBuddyinfo>> map = chatGroupMap;
		Set<TpGroup> set = map.keySet();
		for (TpGroup key : set) {
			if (key.getAttr() == TP_GROUP_ATTR_BALCKLIST) {
				return key.getId();
			}
		}
		return -1;
	}

	public boolean tp_chat_chatgroup_chstatus(TPBuddyinfo info) {
		LinkedList<TPBuddyinfo> list = tp_chat_getfriendslist();
		int i = 0;
		int offlineIndex = 0;
		if(list == null) return false;
		for (TPBuddyinfo infotmp : list) {
			if (infotmp.getStatus() == TP_STATUS_LOGOFF) {
				offlineIndex = i;
			}
		}
		i = 0;
		for (TPBuddyinfo infotmp : list) {
			if (infotmp.getSkyid() == info.getSkyid()) {
				infotmp.setStatus(info.getStatus());
				list.remove(infotmp);
				if (info.getStatus() != TP_STATUS_LOGOFF) {
					list.addFirst(infotmp);
				} else {
					list.add(offlineIndex, infotmp);
				}
				return true;
			}
			i++;
		}
		return false;
	}

	public TPBuddyinfo tp_chat_getBuddyinfo(int skyid) {
		LinkedList<TPBuddyinfo> list = tp_chat_getfriendslist();
		if (list == null)
			return null;
		for (TPBuddyinfo key : list) {
			if (key.getSkyid() == skyid) {
				return key;
			}
		}
		return null;
	}

	private void chat_packet_process(TpPacket pkt) {
		int subid;
		RspData rspdata = new RspData();
		rspdata.setSubid(pkt.getSubid());
		if (pkt.getService() != ProtocolType.ServerId.TP_SERVICE_CHAT) {
			// 出错处理
			Callback(CHAT_SESSIONID_ERR, rspdata, null);
			return;
		}

		subid = pkt.getSubid();
		if (subid > ProtocolType.NotiType.TP_SUBSERVICE_NOTIFICAITON
				&& subid < 0x9000) {
			chat_packet_notification_process(pkt);
			return;
		}
		LinkedList<KeyValue> list = pkt.getList();
		// Map<String, String> map = pkt.getHash();
		// Set<String> set = map.keySet();
		if (pkt.getId() != this.session_id
				&& pkt.getSubid() != ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETGROUP
				&& pkt.getSubid() != ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETFRIENDS
				&& pkt.getSubid() != ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETGROUP
				&& pkt.getSubid() != ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETBUDDIES
				&& pkt.getSubid() != ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_CREATE
				&& pkt.getSubid() != ProtocolType.ChatType.TP_SUBSERVICE_CHAT_MOVEBUDDY
				&& pkt.getSubid() != ProtocolType.ChatType.TP_SUBSERVICE_CHAT_REMOVEBUDDY
				&& pkt.getSubid() != ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETROOMGROUP
				&& pkt.getSubid() != ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SERCHSUGGESTIONS
				&& pkt.getSubid() != ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETSEVERSTR) {
			return;
		}
		switch (subid) {
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETSEVERSTR: {
			Integer id = 0;
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_STORE_MTD_SRVR_ID)) {
					id = Integer.parseInt(value);
				} else if (key.equals(ProtocolType.Key.TP_TAG_ip)) {
					httpAddrmap.put(id, value);
				}
			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETBUDDIES: {// 群成员
			TPBuddyinfo b = new TPBuddyinfo();
			boolean isFirst = true;
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_skyid)) {
					if (b.getSkyid() != -1) {
						LinkedList<TPBuddyinfo> buddiesList = tp_chat_getConferenceBuddiesList(rspdata.getProto_data());
						if (buddiesList != null){
							if(isFirst){
								buddiesList.clear();
								isFirst = false;
							}
							buddiesList.add(b);
						}
						b = new TPBuddyinfo();
//						Callback(CHAT_GETCONFERENCEBUDDIES_OK, rspdata, b);
					}
					b.setSkyid(Integer.parseInt(value));
				} else if (key.equals(ProtocolType.Key.TP_TAG_gid))
					rspdata.setProto_data(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_user))
					b.setName(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_alias))
					b.setAlias(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_salias))
					b.setServer_alias(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_icon))
					b.setAvatarid(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_bstatus))
					b.setStatus(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_WPAPER_PATH))
					b.setAvatar_fpath(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_WPAPER_SRVR))
					b.setAvatar_sid(Integer.parseInt(value));
			}
			if (b.getSkyid() != -1) {
				LinkedList<TPBuddyinfo> buddiesList = tp_chat_getConferenceBuddiesList(rspdata.getProto_data());
				if (buddiesList != null)
					buddiesList.add(b);
				Callback(CHAT_GETCONFERENCEBUDDIES_OK, rspdata, buddiesList);
			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETGROUP: {// 群列表
			TpGroup g = new TpGroup();
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_gid)) {
					if (g.getId() != -1) {
						TpGroup g1 = tp_chat_conferenceIsExist(g.getId());
						if (g1 == null) {
							chatConferenceMap.put(g,
									new LinkedList<TPBuddyinfo>());
						} else if (g1 != null) {
							g1.setName(g.getName());
						}
						g = new TpGroup();
					}
					g.setId(Integer.parseInt(value));
				} else if (key.equals(ProtocolType.Key.TP_TAG_group))
					g.setName(value);
			}
			if (g.getId() != -1) {
				TpGroup g1 = tp_chat_conferenceIsExist(g.getId());
				if (g1 == null) {
					chatConferenceMap.put(g, new LinkedList<TPBuddyinfo>());
				} else if (g1 != null) {
					g1.setName(g.getName());
				}
				Callback(CHAT_GETCONFERENCEGROUP_OK, rspdata, chatConferenceMap);
			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SERCHSUGGESTIONS:
			suggestionBuddiesList.clear();
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SEARCH:// 搜索好友列表
			if (subid == ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SEARCH)
				searchBuddiesList.clear();
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETFRIENDS: {// 好友列表
			TPBuddyinfo b = new TPBuddyinfo();
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_skyid)) {
					if (b.getSkyid() != -1) {
						if (subid == ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SEARCH) {
							searchBuddiesList.add(b);
						} else if (subid == ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SERCHSUGGESTIONS) {
							suggestionBuddiesList.add(b);
						} else {
							LinkedList<TPBuddyinfo> buddiesList = tp_chat_getbuddieslist(rspdata
									.getProto_data());
							if (buddiesList != null)
								buddiesList.add(b);
						}
						b = new TPBuddyinfo();
						// Callback(CHAT_GETBUDDIES_OK, rspdata, b);
					}
					b.setSkyid(Integer.parseInt(value));
				} else if (key.equals(ProtocolType.Key.TP_TAG_gid))
					rspdata.setProto_data(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_user)){
					Log.v("TpChat", "好友列表TP_TAG_user " + value);
					b.setName(value);
				}else if (key.equals(ProtocolType.Key.TP_TAG_alias)){
					Log.v("TpChat", "好友列表TP_TAG_alias " + value);
					b.setAlias(value);
				}else if (key.equals(ProtocolType.Key.TP_TAG_salias))
					b.setServer_alias(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_icon))
					b.setAvatarid(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_bstatus))
					b.setStatus(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_WPAPER_PATH))
					b.setAvatar_fpath(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_WPAPER_SRVR))
					b.setAvatar_sid(Integer.parseInt(value));
			}
			if (b.getSkyid() != -1) {
				if (subid == ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SEARCH) {
					searchBuddiesList.add(b);
					Callback(CHAT_GET_SEARCHBUDDIES_OK, rspdata,
							searchBuddiesList);
				} else if (subid == ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SERCHSUGGESTIONS) {
					suggestionBuddiesList.add(b);
					Callback(CHAT_GET_SEARCHSUGGESTIONS_OK, rspdata,
							suggestionBuddiesList);
				} else {
					LinkedList<TPBuddyinfo> buddiesList = tp_chat_getbuddieslist(rspdata
							.getProto_data());
					if (buddiesList != null)
						buddiesList.add(b);
					Callback(CHAT_GETBUDDIES_OK, rspdata, buddiesList);
				}

			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETGROUP: {// 好友组列表
			TpGroup g = new TpGroup();
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_gid)) {
					if (g.getId() != -1) {
						Callback(CHAT_GETGROUP_OK, rspdata, g);
						chatGroupMap.put(g, new LinkedList<TPBuddyinfo>());
						tp_chat_get_friends(g.getId(), g.getCurrentsize(),
								g.getTotalsize());// 请求好友列表
						g = new TpGroup();
					}
					g.setId(Integer.parseInt(value));
				} else if (key.equals(ProtocolType.Key.TP_TAG_group))
					g.setName(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_gnum))
					g.setCurrentsize(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_gtotal))
					g.setTotalsize(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_gattr))
					g.setAttr(Integer.parseInt(value));
			}
			if (g.getId() != -1) {
				Callback(CHAT_GETGROUP_OK, rspdata, g);
				chatGroupMap.put(g, new LinkedList<TPBuddyinfo>());
				tp_chat_get_friends(g.getId(), g.getCurrentsize(),
						g.getTotalsize());// 请求好友列表
				tp_conference_get_conferences();// 获取群列表
			}

		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETROOMGROUP: {// 聊天室列表
			TpGroup g = new TpGroup();
			Log.v("TpChat", "聊天室列表 list.size " + list.size());
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;

				if (key.equals(ProtocolType.Key.TP_TAG_gid)) {
					if (g.getId() != -1) {
						chatRoomList.add(g);
						g = new TpGroup();
						// Log.v("TpChat", "NEEWWW聊天室列表 key " + key +
						// " value = " + value);
						// Callback(CHAT_ROOM_GETLIST_OK, rspdata,
						// chatRoomList);
					}
					// Log.v("TpChat", "NEEWWW聊天室列表 key " + key + " g = " + g);
					g.setId(Integer.parseInt(value));
				} else if (key.equals(ProtocolType.Key.TP_TAG_group)) {
					Log.v("TpChat", "-----  value = " + value);
					g.setName(value);

				} else if (key.equals(ProtocolType.Key.TP_TAG_gnum))
					g.setCurrentsize(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_gtotal))
					g.setTotalsize(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_gdes))
					g.setDes(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_alias))
					g.setAlias(value);
			}
			if (g.getId() != -1) {
				chatRoomList.add(g);
				Callback(CHAT_ROOM_GETLIST_OK, rspdata, chatRoomList);
			}
		}
			break;

		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_AUTH:/** < . */
		{
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_err))
					rspdata.setErrcode(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_errstr))
					rspdata.setString(value);
			}
			if (rspdata.getErrcode() == ProtocolType.TP_ERRCODE_OK) {
				Callback(CHAT_AUTH_OK, rspdata, null);
				isneedKeepAlive = true;
				tp_chat_connectchat();
				tp_chat_getofflinemsg();
				tp_chat_gethttpaddr();
				tp_chat_getofflinemsg();
				tp_chat_get_group();// 获取好友组
				tp_chat_get_chatroom();// 获取聊天室列表
				tp_chat_setstatus(TP_STATUS_AVAILABLE);
				tp_chat_search_suggestions();// 获取推荐好友列表
			} else {
				Callback(CHAT_AUTH_ERR, rspdata, null);
			}

		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETINFO: {
			TPBuddyinfo info = new TPBuddyinfo();

			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_err))
					rspdata.setErrcode(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_errstr))
					rspdata.setString(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_gid))
					rspdata.setProto_data(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_user))
					info.setName(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_alias))
					info.setAlias(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_salias))
					info.setServer_alias(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_icon))
					info.setAvatarid(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_bstatus))
					info.setStatus(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_sex))
					info.setSex(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_mcc))
					info.setCountry(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_sign))
					info.setSignature(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_birth))
					info.setBirthday(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_lanstr))
					info.setLanstr(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_lan))
					info.setLan(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_WPAPER_PATH))
					info.setAvatar_fpath(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_WPAPER_SRVR))
					info.setAvatar_sid(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_LCTN))////本地位置
					info.setCountry(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_pnum))
					info.setPnum(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_qusiontype))
					info.setBindingStatus(Integer.parseInt(value));
			}
			if (rspdata.getErrcode() == ProtocolType.TP_ERRCODE_OK) {
				Callback(CHAT_GETINFO_OK, rspdata, info);
			} else {
				Callback(CHAT_GETINFO_ERR, rspdata, null);
			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_GET: {
			TPPrivate info = new TPPrivate();
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_PRT_DETAIL_DIS))
					info.setDisinfo(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_SEARCHED))
					info.setSrched(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_STNG_MSG))
					info.setStrgmsg(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_AUTO_ACTIVE))
					info.setAutoact(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_WPAPER_ID))
					info.setBg(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_WPAPER_SRVR))
					info.setSid(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_PRT_WPAPER_PATH))
					info.setFpath(value);
			}
			tpp = info;
			Callback(CHAT_GET_PRIVATE_OK, rspdata, info);
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_SET: {
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_err))
					rspdata.setErrcode(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_errstr))
					rspdata.setString(value);
			}
			if (rspdata.getErrcode() == ProtocolType.TP_ERRCODE_OK) {
				Callback(CHAT_SET_PRIVATE_OK, rspdata, null);
			} else {
				Callback(CHAT_SET_PRIVATE_ERR, rspdata, null);
			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_STORE_MTD_GET:
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_STORE_MTD_ME_GET:
			// {
			// TPWidget btmp = {0};
			// TPWidget*b = &btmp;
			// while (l) {
			// pair = l->data;
			// if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_NAME)){
			// b->name = ((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_DESC)){
			// b->desc = ((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_PRICE)){
			// b->price = atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_CRNCY)){
			// b->symbol = ((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_TYPE)){
			// b->type = atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_ID)){
			// b->id = atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_COUNT)){
			// b->scount = atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_SRVR_PATH_D)){
			// b->fPath = ((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_SRVR_PATH_T)){
			// b->iPath = ((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_TIME)){
			// b->cTime = atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_SRVR_ID)){
			// b->sId = atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_STORE_MTD_BUY)){
			// b->buy = atoi((gchar *) pair->value);
			// }
			// l = l->next;
			// }
			// if(TP_SUBSERVICE_CHAT_STORE_MTD_GET == pkt.getSubid()){
			// CHAT_CB_EVENT(pChat,CHAT_STORE_GET_OK,&common_data,b);
			// }
			// else if(TP_SUBSERVICE_CHAT_STORE_MTD_ME_GET == pkt.getSubid()){
			// CHAT_CB_EVENT(pChat,CHAT_STORE_ME_GET_OK,&common_data,b);
			// }
			// }
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_STORE_MTD_PURCHASE:
			// {
			// TPPrivate btmp = {0};
			// TPPrivate*info = &btmp;
			// while (l) {
			// pair = l->data;
			// if(0 == strcmp(pair->keystr,TP_TAG_err)){
			// common_data.errcode = atoi(pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_errstr)){
			// common_data.string = ((gchar *) pair->value);
			// }
			// l = l->next;
			// }
			// if(common_data.errcode == TP_ERRCODE_OK){
			// CHAT_CB_EVENT(pChat,CHAT_STORE_PURCHASE_OK,&common_data,info);
			// }
			// else
			// CHAT_CB_EVENT(pChat,CHAT_STORE_PURCHASE_ERR,&common_data,NULL);
			// }
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_CHANGEPWD: {
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_err))
					rspdata.setErrcode(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_errstr))
					rspdata.setString(value);
			}
			if (rspdata.getErrcode() == ProtocolType.TP_ERRCODE_OK) {
				Callback(CHAT_CHPWD_OK, rspdata, null);
			} else {
				Callback(CHAT_CHPWD_ERR, rspdata, null);
			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_REMOVEBUDDY: {
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_err))
					rspdata.setErrcode(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_errstr))
					rspdata.setString(value);
			}
			if (rspdata.getErrcode() == ProtocolType.TP_ERRCODE_OK) {
				Callback(CHAT_REMOVEBUDDY_OK, rspdata, null);
			} else {
				Callback(CHAT_REMOVEBUDDY_ERR, rspdata, null);
			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_MOVEBUDDY: {
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_err))
					rspdata.setErrcode(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_errstr))
					rspdata.setString(value);
			}
			if (rspdata.getErrcode() == ProtocolType.TP_ERRCODE_OK) {
				Callback(CHAT_MOVEBUDDY_OK, rspdata, null);
			} else {
				Callback(CHAT_MOVEBUDDY_ERR, rspdata, null);
			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_SNS_DELSTATUS:
		case ProtocolType.ChatType.TP_SUBSERVICE_SNS_ADDSTATUS:
		case ProtocolType.ChatType.TP_SUBSERVICE_SNS_DELRES:
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SETINFO:/** < . */
		{
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_err))
					rspdata.setErrcode(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_errstr))
					rspdata.setString(value);
			}
			if (rspdata.getErrcode() == ProtocolType.TP_ERRCODE_OK) {
				Callback(CHAT_SETINFO_OK, rspdata, null);
			} else {
				Callback(CHAT_SETINFO_ERR, rspdata, null);
			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_QUIT:/** < .退出群*/
		{
			
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_CREATE:/** < .群创建 */
		{
			TpGroup g = new TpGroup();
			TpGroup greal = null;
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_err)) {
					rspdata.setErrcode(Integer.parseInt(value));
				} else if (key.equals(ProtocolType.Key.TP_TAG_errstr)) {
					rspdata.setString(value);
				} else if (key.equals(ProtocolType.Key.TP_TAG_gid)) {
					g.setId(Integer.parseInt(value));
				} else if (key.equals(ProtocolType.Key.TP_TAG_group))
					g.setName(value);
			}

			TpGroup g1 = tp_chat_conferenceIsExist(g.getId());
			if (g1 == null) {
				greal = g;
				chatConferenceMap.put(g, new LinkedList<TPBuddyinfo>());
			} else if (g1 != null) {
				greal = g1;
				g1.setName(g.getName());
			}
			if (rspdata.getErrcode() == ProtocolType.TP_ERRCODE_OK) {
				Callback(CHAT_CREATECONFERENCE_OK, rspdata, greal);
			} else {
				Callback(CHAT_CREATECONFERENCE_ERR, rspdata, null);
			}
		}
			break;
		case ProtocolType.ChatType.TP_SUBSERVICE_SNS_GETURL:
			// {
			// tp_sns_data snsdata = {0};
			// GList *ltmp = pkt->hash;
			// int cssFlag = -1;
			// while (ltmp) {
			// pair = ltmp->data;
			// if(0 == strcmp(pair->keystr,TP_TAG_SNS_css)){
			// cssFlag = atoi(pair->value);
			// break;
			// }
			// ltmp = ltmp->next;
			// }
			// if(cssFlag == TRUE){
			// tp_sns_css css = {0};
			// int index = -1;
			// int gindex = -1;
			// snsdata.datatype = 0;//表示css
			// snsdata.u.css = &css;
			//
			// while (l) {
			// pair = l->data;
			// if(0 == strcmp(pair->keystr,TP_TAG_err)){
			// common_data.errcode = atoi(pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_errstr)){
			// common_data.string = ((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_type)){
			// css.type = atoi(pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_next)){
			// css.next= ((gchar *) pair->value);
			// } else if(0 == strcmp(pair->keystr,TP_TAG_SNS_pre)){
			// css.pre= ((gchar *) pair->value);
			// } else if(0 == strcmp(pair->keystr,TP_TAG_SNS_item)){
			// if(gindex+1 >= MAX_SNS_GROUP) break;
			// ++gindex;
			// index = -1;
			// } else if(0 == strcmp(pair->keystr,TP_TAG_SNS_id)){
			// if(index+1 >= MAX_SNS_CRL) break;
			// css.crl[gindex][++index].id= atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_ctrl)){
			// css.crl[gindex][index].url= ((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_link)){
			// css.crl[gindex][index].link= ((gchar *) pair->value);
			// }
			// l = l->next;
			// }
			// if(common_data.errcode != TP_ERRCODE_OK){
			// CHAT_CB_EVENT(pChat,CHAT_SNS_URL_ERR,&common_data,&snsdata);
			// }
			// else
			// CHAT_CB_EVENT(pChat,CHAT_SNS_URL_CSS_OK ,&common_data,&snsdata);
			// }
			// else if (cssFlag == FALSE ){
			// tp_sns_crl_headdata head = {0};
			// tp_sns_crl_itemdata item = {0};
			// int headsent = FALSE;
			// int index = -1;
			// while (l) {
			// pair = l->data;
			// if(0 == strcmp(pair->keystr,TP_TAG_err)){
			// common_data.errcode = atoi(pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_errstr)){
			// common_data.string = ((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_type)){
			// head.type = atoi(pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_subtype)){
			// head.subtype= atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_next)){
			// head.next= ((gchar *) pair->value);
			// } else if(0 == strcmp(pair->keystr,TP_TAG_SNS_pre)){
			// head.pre= ((gchar *) pair->value);
			// } else if(0 == strcmp(pair->keystr,TP_TAG_SNS_id)){
			// head.id= atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_total)){
			// head.total = atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_attri)){
			// /*暂时不需要干什么*/
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_count)){
			// head.count = atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_page)){
			// head.page = atoi((gchar *) pair->value);
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_item)){
			// if(!headsent){
			// snsdata.datatype = 1;//表示表示控件头数据
			// snsdata.u.crlhead = &head;
			// if(common_data.errcode != TP_ERRCODE_OK){
			// CHAT_CB_EVENT(pChat,CHAT_SNS_URL_ERR,&common_data,&snsdata);
			// }
			// else{
			// CHAT_CB_EVENT(pChat,CHAT_SNS_URL_CRLHEADDATA_OK,&common_data,&snsdata);
			// }
			// headsent = TRUE;
			// }
			// if(index>=0){
			// snsdata.datatype = 2;//表示控件的item数据
			// snsdata.u.crlitem = &item;
			// if(common_data.errcode != TP_ERRCODE_OK){
			// CHAT_CB_EVENT(pChat,CHAT_SNS_URL_ERR,&common_data,&snsdata);
			// }
			// else{
			// CHAT_CB_EVENT(pChat,CHAT_SNS_URL_CRLITEMDATA_OK,&common_data,&snsdata);
			// }
			//
			// }
			// memset(&item,0,sizeof(tp_sns_crl_itemdata));
			// index = -1;
			// }else if(0 == strcmp(pair->keystr,TP_TAG_SNS_p)){
			// item.p[++index] = ((gchar *) pair->value);
			// }
			// l = l->next;
			// }
			// if(index>=0){
			// snsdata.datatype = 2;//表示控件的item数据
			// snsdata.u.crlitem = &item;
			// if(common_data.errcode != TP_ERRCODE_OK){
			// CHAT_CB_EVENT(pChat,CHAT_SNS_URL_ERR,&common_data,&snsdata);
			// }
			// else{
			// CHAT_CB_EVENT(pChat,CHAT_SNS_URL_CRLITEMDATA_OK,&common_data,&snsdata);
			// }
			// }
			// }
			// }
			break;
		// TP_SUBSERVICE_CONFERENCE_CREATE
		}
		return;
	}

	private void chat_packet_notification_process(TpPacket pkt) {
		int subid;
		RspData rspdata = new RspData();
		rspdata.setSubid(pkt.getSubid());
		subid = pkt.getSubid();
		// common_data.session_id = pkt->id;
		TpNotiNode noti_node = new TpNotiNode();
		noti_node.setType(subid);
		LinkedList<KeyValue> list = pkt.getList();
		switch (subid) {
		case ProtocolType.NotiType.TP_NOTIFICATION_CHAT_DENYUPLOADFILE: // 拒绝上传文件
		case ProtocolType.NotiType.TP_NOTIFICATION_TEXT: { // 系统公告
			String defaultstr = null;
			String curstr = null;
			String string = null;
			int curlan = -1;

			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_lan))
					curlan = (Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_lanstr))
					curstr = (value);
				else if (key.equals(ProtocolType.Key.TP_TAG_defaultlan))
					;
				else if (key.equals(ProtocolType.Key.TP_TAG_defaultstr))
					defaultstr = (value);
			}
			string = defaultstr;
			// 这里有问题。
			// if(curlan == UserInfo.getLang())
			// string = curstr;
			noti_node.setText(string);
			Callback(CHAT_NOTIFICATION_OK, rspdata, noti_node);
		}
			break;
		case ProtocolType.NotiType.TP_NOTIFICATION_CHAT_MSG:
		case ProtocolType.NotiType.TP_NOTIFICATION_CONFERENECT_MSG: {// 消息(群消息，点对点消息)
			int curlan = -1;
			int gtype = -1;
			tp_msg_node node = noti_node.new tp_msg_node();

			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_skyid)) {
					if (node.getSkyid() != -1) {
						noti_node.setMsg_node(node);
						Callback(CHAT_NOTIFICATION_OK, rspdata, noti_node);
					}
					node.setSkyid(Integer.parseInt(value));
				} else if (key.equals(ProtocolType.Key.TP_TAG_msg))
					node.setMsg(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_gtype))
					node.setMsgtype(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_gid))
					node.setGid(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_alias))
					node.setAlias(value);
			}
			if (node.getSkyid() != -1) {
				noti_node.setMsg_node(node);
				Callback(CHAT_NOTIFICATION_OK, rspdata, noti_node);
			}

		}
			break;
		case ProtocolType.NotiType.TP_NOTIFICATION_CHAT_PREMITED: // 同意
		case ProtocolType.NotiType.TP_NOTIFICATION_CHAT_DENIED: // 拒绝
		case ProtocolType.NotiType.TP_NOTIFICATION_CHAT_BUDDYREQ: // 请求加好友
		case ProtocolType.NotiType.TP_NOTIFICATION_CHAT_BSTATUS: { // 好友状态（上下线）
			TPBuddyinfo b = new TPBuddyinfo();
			tp_req_node reqnode = noti_node.new tp_req_node();
			reqnode.setBuddy(b);
			noti_node.setReq_node(reqnode);
			int curlan = -1;
			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_skyid)) {
					b.setSkyid(Integer.parseInt(value));
				} else if (key.equals(ProtocolType.Key.TP_TAG_gid))
					rspdata.setProto_data(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_user))
					b.setName(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_alias))
					b.setAlias(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_salias))
					b.setServer_alias(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_icon))
					b.setAvatarid(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_bstatus))
					b.setStatus(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_lan))
					curlan = Integer.parseInt(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_lanstr)) {
					;
				} else if (key.equals(ProtocolType.Key.TP_TAG_defaultlan))
					;
				else if (key.equals(ProtocolType.Key.TP_TAG_defaultstr))
					reqnode.setText(value);
			}
			if (b.getSkyid() != -1) {
				if(ProtocolType.NotiType.TP_NOTIFICATION_CHAT_BSTATUS == subid){
					if(tp_chat_chatgroup_chstatus(b)){
						Callback(CHAT_NOTIFICATION_OK, rspdata, noti_node);
					}
				}else
					Callback(CHAT_NOTIFICATION_OK, rspdata, noti_node);
			}
		}
			break;
		case ProtocolType.NotiType.TP_NOTIFICATION_CHAT_PERMITUPLOADFILE: // 同意上传
		case ProtocolType.NotiType.TP_NOTIFICATION_CHAT_DOWNLOADRES: { // 下载资源
			int curlan = -1;
			tp_res_node resnode = noti_node.new tp_res_node();
			noti_node.setRes_node(resnode);

			for (KeyValue kv : list) {
				String key = kv.key;
				String value = kv.value;
				if (key.equals(ProtocolType.Key.TP_TAG_lan))
					curlan = Integer.parseInt(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_lanstr)) {
					;
				} else if (key.equals(ProtocolType.Key.TP_TAG_defaultlan))
					;
				else if (key.equals(ProtocolType.Key.TP_TAG_defaultstr))
					resnode.setText(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_skyid))
					resnode.setSkyid(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_user))
					resnode.setName(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_alias))
					resnode.setAlias(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_fname))
					resnode.setFilename(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_fnamelen))
					resnode.setFilelen(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_STORE_MTD_SRVR_PATH_T))
					resnode.setIpath(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_STORE_MTD_SRVR_PATH_D))
					resnode.setFpath(value);
				else if (key.equals(ProtocolType.Key.TP_TAG_gid))
					resnode.setGoup_id(Integer.parseInt(value));
				else if (key.equals(ProtocolType.Key.TP_TAG_STORE_MTD_SRVR_ID))
					resnode.setServer_id(Integer.parseInt(value));
			}
			Callback(CHAT_NOTIFICATION_OK, rspdata, noti_node);
		}
			break;
		}
		return;
	}

	private void CallbackBroadCast(int result, RspData resultdata, Object obj) {
		Message msg = new Message();
		msg.what = result;
		Bundle bundle = new Bundle();
		bundle.putInt("result", result);
		bundle.putSerializable("resultdata", resultdata);
		bundle.putSerializable("object", (Serializable) obj);
		msg.setData(bundle);
		PlayTalkService.getInstance().serviceHandler.sendMessage(msg);
		// Intent intent = new Intent();
		// if(result == CHAT_ROOM_GETLIST_OK)
		// intent.setAction(PlayTalkService.COMMUNITY_BRODCASE);
		// intent.putExtra("result", result);
		// intent.putExtra("resultdata", resultdata);
		// intent.putExtra("object", (Serializable) obj);
		// context.sendBroadcast(intent);
	}

	private void Callback(int result, RspData resultdata, Object obj) {
		Log.v("TpChat", "Callback = " + result);
		switch (result) {
		case CHAT_SESSIONID_ERR:
		case CHAT_NETWORK_ERR: {
			ProtocolList list = getProtocolPacketSendList();
			if (list != null) {
				SendNode sendnode = list.tp_data_firstnode();
				if (sendnode!=null&&sendnode.getCallBack() != null) {
					sendnode.getCallBack().Callback(result, resultdata, obj);
				}
			}
		}
			break;
		case CHAT_AUTH_OK:
		case CHAT_AUTH_ERR:
		case CHAT_AUTH_TIMEOUT:
			if (callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_AUTH) != null) {
				callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_AUTH).Callback(result, resultdata, obj);
				return;
			}
			break;
		case CHAT_CHPWD_OK:
		case CHAT_CHPWD_ERR:
		case CHAT_CHPWD_TIMEOUT:
			if (callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_CHANGEPWD) != null) {
				callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_CHANGEPWD).Callback(result, resultdata, obj);
				return;
			}
			break;
		case CHAT_SETINFO_OK:
		case CHAT_SETINFO_ERR:
		case CHAT_SETINFO_TIMEOUT:
			if (callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_CHANGEPWD) != null) {
				callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_CHANGEPWD).Callback(result, resultdata, obj);
				return;
			}
			break;
		case CHAT_REMOVEBUDDY_OK:
		case CHAT_REMOVEBUDDY_ERR:
		case CHAT_MOVEBUDDY_OK:
		case CHAT_MOVEBUDDY_ERR:
		case CHAT_ROOM_GETLIST_OK:

			break;
		case CHAT_GETINFO_OK:
		case CHAT_GETINFO_ERR:
		case CHAT_GETINFO_TIMEOUT:
			if (callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETINFO) != null) {
				callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_GETINFO).Callback(result, resultdata, obj);
				return;
			}
			break;
		case CHAT_CREATECONFERENCE_OK:
		case CHAT_CREATECONFERENCE_ERR:
		case CHAT_CONFERENCE_CREATE_TIMEOUT:
			if (callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_CREATE) != null) {
				callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_CREATE).Callback(result, resultdata, obj);
				return;
			}
			break;
		case CHAT_NOTIFICATION_OK:
		case CHAT_GETGROUP_OK:
		case CHAT_GETBUDDIES_OK:
		case CHAT_GETCONFERENCEGROUP_OK:
		case CHAT_GETCONFERENCEGROUP_ERR:
			break;
		case CHAT_GETCONFERENCEBUDDIES_OK:
		case CHAT_GETCONFERENCEBUDDIES_ERR:
			if (callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETBUDDIES) != null) {
				callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CONFERENCE_GETBUDDIES).Callback(result, resultdata, obj);
				return;
			}
			break;
		case CHAT_GET_PRIVATE_OK:
		case CHAT_GET_PRIVATE_TIMEOUT:
		case CHAT_GET_PRIVATE_ERR:
			if (callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_GET) != null) {
				callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_GET).Callback(result, resultdata, obj);
				return;
			}
			break;
		case CHAT_SET_PRIVATE_OK:
		case CHAT_SET_PRIVATE_TIMEOUT:
		case CHAT_SET_PRIVATE_ERR:
			if (callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_SET) != null) {
				callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_PRIVATE_SET).Callback(result, resultdata, obj);
				return;
			}
			break;
		case CHAT_GET_SEARCHBUDDIES_OK:
		case CHAT_GET_SEARCHBUDDIES_ERR:
		case CHAT_GET_SEARCHBUDDIES_TIMEOUT:
			if (callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SEARCH) != null) {
				callBackMap.get(ProtocolType.ChatType.TP_SUBSERVICE_CHAT_SEARCH).Callback(result, resultdata, obj);
				return;
			}
			break;
		case CHAT_STORE_GET_OK:
		case CHAT_STORE_GET_TIMEOUT:
		case CHAT_STORE_GET_ERR:
			break;
		case CHAT_STORE_ME_GET_OK:
		case CHAT_STORE_ME_GET_TIMEOUT:
		case CHAT_STORE_ME_GET_ERR:
			break;
		case CHAT_STORE_PURCHASE_OK:
		case CHAT_STORE_PURCHASE_TIMEOUT:
		case CHAT_STORE_PURCHASE_ERR:
			break;
		case CHAT_SNS_START:
		case CHAT_SNS_DELSTATUS_OK:
		case CHAT_SNS_DELSTATUS_ERR:
		case CHAT_SNS_DELSTATUS_TIMEOUT:
			break;
		case CHAT_SNS_DELRES_OK:
		case CHAT_SNS_DELRES_ERR:
		case CHAT_SNS_DELRES_TIMEOUT:
			break;
		case CHAT_SNS_ADDSTATUS_OK:
		case CHAT_SNS_ADDSTATUS_ERR:
		case CHAT_SNS_ADDSTATUS_TIMEOUT:
			break;
		case CHAT_SNS_URL_CSS_OK:
		case CHAT_SNS_URL_CRLHEADDATA_OK:
		case CHAT_SNS_URL_CRLITEMDATA_OK:
		case CHAT_SNS_URL_ERR:
		case CHAT_SNS_URL_TIMEOUT:
			break;

		}

		CallbackBroadCast(result, resultdata, obj);
	}
}
